package com.anti.modular.owner.ownerleasebilldetail.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.commom.core.utils.DecimalUtils;
import com.anti.core.context.login.LoginContextHolder;
import com.anti.jdbc.DBUtils;
import com.anti.jdbc.support.page.PageParam;
import com.anti.jdbc.support.page.PageResult;
import com.anti.commom.core.exception.BusinessException;
import com.anti.modular.owner.ownerleasebilldetail.entity.OwnerLeaseBillDetail;
import com.anti.modular.owner.ownerleasebilldetail.enums.OwnerLeaseBillDetailExceptionEnum;
import com.anti.modular.owner.ownerleasebilldetail.param.OwnerLeaseBillDetailParam;
import com.anti.modular.owner.ownerleasebilldetail.service.OwnerLeaseBillDetailService;
import com.anti.modular.owner.ownerleasefees.entity.OwnerLeaseFees;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 業主賬單明細service接口实现类
 *
 * @author anti
 * @date 2021-05-20 14:26:44
 */
@Service
public class OwnerLeaseBillDetailServiceImpl implements OwnerLeaseBillDetailService {
    @Resource
    DBUtils dbUtils;

    @Override
    public OwnerLeaseBillDetail getById(OwnerLeaseBillDetailParam ownerLeaseBillDetailParam){
        String sql="select * from owner_lease_bill_detail where id =? ";
        return dbUtils.get(sql,OwnerLeaseBillDetail.class,ownerLeaseBillDetailParam.getId());
    }

    @Override
    public BigDecimal getAmountByBillId(Long billId) {
        BigDecimal decimal=new BigDecimal(0.00);
        List<OwnerLeaseBillDetail> query =this.getListByBillIdForRead(billId);
        OwnerLeaseBillDetail ownerLeaseBillDetail=null;
        for (int i = 0; i < query.size(); i++) {
            ownerLeaseBillDetail = query.get(i);
            int billInOutType = ownerLeaseBillDetail.getBillInOutType();
            BigDecimal amount = ownerLeaseBillDetail.getAmount();
            if (billInOutType==2){
                decimal = DecimalUtils.add(decimal,amount.negate());
            }else {
                decimal = DecimalUtils.add(decimal,amount);
            }
        }
        return decimal;
    }

    @Override
    public PageResult page(PageParam pageParam,OwnerLeaseBillDetailParam ownerLeaseBillDetailParam) {
        StringBuilder sb=new StringBuilder("select * from owner_lease_bill_detail where 1=1 ");
        ArrayList<Object> param = CollectionUtil.newArrayList();
        if (ObjectUtil.isNotNull(ownerLeaseBillDetailParam)) {
            // 根据費用類別名稱 查询
            if (ObjectUtil.isNotEmpty(ownerLeaseBillDetailParam.getRentAmountName())) {
               sb.append(" and rent_amount_name = ?");
               param.add(ownerLeaseBillDetailParam.getRentAmountName());
            }
        }
        return dbUtils.query(pageParam,sb.toString(),param.toArray());
    }

    @Override
    public List<OwnerLeaseBillDetail> list(OwnerLeaseBillDetailParam ownerLeaseBillDetailParam) {
        String sql="";
        return dbUtils.query(sql,OwnerLeaseBillDetail.class);
    }

    @Override
    public List<OwnerLeaseBillDetail> getListByBillIdForRead(Long billId) {
        String sql="select * from owner_lease_bill_detail where owner_bill_id =? and status =0";
        return dbUtils.query(sql,OwnerLeaseBillDetail.class,billId);
    }

    @Override
    public List<OwnerLeaseBillDetail> getListByBillIdForEditByType(Long leaseId, int type) {
        StringBuilder sql= new StringBuilder("select * from owner_lease_bill_detail where owner_bill_id =? and status =0");
        if (type==1){
            sql.append(" and type in (1,2,3)");
        }else if (type==2){
            sql.append(" and type=4 ");
        }if (type==3){
            sql.append(" and type=5");
        }
        return dbUtils.query(sql.toString(),OwnerLeaseBillDetail.class,leaseId);
    }

    @Override
    public void add(OwnerLeaseBillDetailParam ownerLeaseBillDetailParam) {
        OwnerLeaseBillDetail ownerLeaseBillDetail = new OwnerLeaseBillDetail();
        BeanUtil.copyProperties(ownerLeaseBillDetailParam, ownerLeaseBillDetail);
        dbUtils.insert(ownerLeaseBillDetail);
    }

    @Override
    public void addGdfy(List<OwnerLeaseFees> ownerLeaseFees, Long billId) {
        OwnerLeaseFees leaseFees = null;
        OwnerLeaseBillDetail ownerLeaseBillDetail=null;
        Date date = new Date();
        Long sysLoginUserId = LoginContextHolder.me().getSysLoginUserId();
        for (int i = 0; i < ownerLeaseFees.size(); i++) {
            leaseFees = ownerLeaseFees.get(i);
            ownerLeaseBillDetail = new OwnerLeaseBillDetail();
            ownerLeaseBillDetail.setOwnerBillId(billId);
            ownerLeaseBillDetail.setBillInOutType(2);
            ownerLeaseBillDetail.setType(3);
            ownerLeaseBillDetail.setRentAmountCategory(leaseFees.getFeesId());
            ownerLeaseBillDetail.setRentAmountName(leaseFees.getFeesName());
            ownerLeaseBillDetail.setAmount(leaseFees.getAmount());
            ownerLeaseBillDetail.setStatus(0);
            ownerLeaseBillDetail.setCreateTime(date);
            ownerLeaseBillDetail.setCreateUser(sysLoginUserId);
            dbUtils.insert(ownerLeaseBillDetail);
        }
    }

    /**
     *
     * @param ownerLeaseFees
     * @param billId
     * @param type   4臨時支出  5臨時收入
     */
    @Override
    public void addGdfyByType(List<OwnerLeaseBillDetail> ownerLeaseFees, Long billId, int type) {
        OwnerLeaseBillDetail leaseFees = null;
        OwnerLeaseBillDetail ownerLeaseBillDetail=null;
        Date date = new Date();
        Long sysLoginUserId = LoginContextHolder.me().getSysLoginUserId();
        for (int i = 0; i < ownerLeaseFees.size(); i++) {
            leaseFees = ownerLeaseFees.get(i);
            ownerLeaseBillDetail = new OwnerLeaseBillDetail();
            ownerLeaseBillDetail.setOwnerBillId(billId);
            if (type==5){
                ownerLeaseBillDetail.setBillInOutType(1);
            }else {
                ownerLeaseBillDetail.setBillInOutType(2);
            }
            ownerLeaseBillDetail.setType(type);
            ownerLeaseBillDetail.setRentAmountCategory(leaseFees.getRentAmountCategory());
            ownerLeaseBillDetail.setRentAmountName(leaseFees.getRentAmountName());
            ownerLeaseBillDetail.setAmount(leaseFees.getAmount());
            ownerLeaseBillDetail.setStatus(0);
            ownerLeaseBillDetail.setCreateTime(date);
            ownerLeaseBillDetail.setCreateUser(sysLoginUserId);
            dbUtils.insert(ownerLeaseBillDetail);
        }
    }

    @Override
    public void addYj(BigDecimal deposit, Long billId,int type) {
        OwnerLeaseBillDetail ownerLeaseBillDetail = new OwnerLeaseBillDetail();
        ownerLeaseBillDetail.setOwnerBillId(billId);
        if (type ==1){
            ownerLeaseBillDetail.setBillInOutType(2);
        }else {
            ownerLeaseBillDetail.setBillInOutType(1);
        }
        ownerLeaseBillDetail.setType(2);
        ownerLeaseBillDetail.setRentAmountName("押金");
        ownerLeaseBillDetail.setAmount(deposit);
        ownerLeaseBillDetail.setStatus(0);
        ownerLeaseBillDetail.setCreateTime(new Date());
        ownerLeaseBillDetail.setCreateUser(LoginContextHolder.me().getSysLoginUserId());
        dbUtils.insert(ownerLeaseBillDetail);
    }

    @Override
    public void addZj(BigDecimal deposit, Long billId) {
        OwnerLeaseBillDetail ownerLeaseBillDetail = new OwnerLeaseBillDetail();
        ownerLeaseBillDetail.setOwnerBillId(billId);
        ownerLeaseBillDetail.setBillInOutType(2);
        ownerLeaseBillDetail.setType(1);
        ownerLeaseBillDetail.setRentAmountName("租金");
        ownerLeaseBillDetail.setAmount(deposit);
        ownerLeaseBillDetail.setStatus(0);
        ownerLeaseBillDetail.setCreateTime(new Date());
        ownerLeaseBillDetail.setCreateUser(LoginContextHolder.me().getSysLoginUserId());
        dbUtils.insert(ownerLeaseBillDetail);
    }

    public void delete(OwnerLeaseBillDetailParam ownerLeaseBillDetailParam) {
        String sql="delete  from owner_lease_bill_detail where id =? ";
        dbUtils.execSql(sql,ownerLeaseBillDetailParam.getId());
    }

    @Override
    public void deleteByOwnerBillId(Long ownerBillId) {
        String sql="update  owner_lease_bill_detail set status=2 where owner_bill_id =? and status=0";
        dbUtils.execSql(sql,ownerBillId);
    }

    @Override
    public void deleteXuByOwnerBillId(Long ownerBillId) {
        Date date = new Date();
        Long sysLoginUserId = LoginContextHolder.me().getSysLoginUserId();
        String detail="update owner_lease_bill_detail set status=2,update_time=?,update_user=? where owner_bill_id =? and status=0";
        dbUtils.execSql(detail,date,sysLoginUserId,ownerBillId);
        String main="update owner_lease_bill set status=2,update_time=?,update_user=? where id=? and status=0";
        dbUtils.execSql(main,date,sysLoginUserId,ownerBillId);
    }

    @Override
    public void deleteByOwnerBillIdOnType(Long ownerBillId, int type) {
        String sql="update  owner_lease_bill_detail set status=2 where owner_bill_id =? and type=?";
        dbUtils.execSql(sql,ownerBillId,type);
    }

    public void edit(OwnerLeaseBillDetailParam ownerLeaseBillDetailParam) {
        OwnerLeaseBillDetail ownerLeaseBillDetail = this.queryOwnerLeaseBillDetail(ownerLeaseBillDetailParam);
        BeanUtil.copyProperties(ownerLeaseBillDetailParam, ownerLeaseBillDetail);
        dbUtils.update(ownerLeaseBillDetail);
    }

    @Override
    public void editQsFy(List<OwnerLeaseBillDetail> yjList) {
        String updateSql="update owner_lease_bill_detail set amount=?,update_time=?,update_user=? where id =?";
        Date date = new Date();
        Long sysLoginUserId = LoginContextHolder.me().getSysLoginUserId();
        yjList.forEach(item->{
            dbUtils.execSql(updateSql,item.getAmount(),date,sysLoginUserId,item.getId());
        });
    }

    @Override
    public OwnerLeaseBillDetail detail(OwnerLeaseBillDetailParam ownerLeaseBillDetailParam) {
        return this.queryOwnerLeaseBillDetail(ownerLeaseBillDetailParam);
    }

    /**
     * 获取業主賬單明細
     *
     * @author anti
     * @date 2021-05-20 14:26:44
     */
    private OwnerLeaseBillDetail queryOwnerLeaseBillDetail(OwnerLeaseBillDetailParam ownerLeaseBillDetailParam) {
        OwnerLeaseBillDetail ownerLeaseBillDetail = this.getById(ownerLeaseBillDetailParam);
        if (ObjectUtil.isNull(ownerLeaseBillDetail)) {
            throw new BusinessException(OwnerLeaseBillDetailExceptionEnum.NOT_EXIST);
        }
        return ownerLeaseBillDetail;
    }


}
